package hotel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

import java.util.ArrayList;

public class ownerApplication implements ActionListener {
	protected static JFrame testframe; // static so the class progressBar can								// dispose of it.
	private JPanel panel;
	private JFrame ownerFrame;
	private JTextField hotelField, roomsField;
	private JButton addHotelButton, removeHotelButton;
	private String ownerName;
	private Owner owner;

	/**
	 * This constructor creates a login window, and also adds components to the
	 * content pane.
	 */
	public ownerApplication(String username) 
	{
		ownerName = username;
		//Find owner in database
		ArrayList<Owner> ownerList = loginWindow.database.getOwnerList();
		for(int i = 0; i < ownerList.size(); i++)
			if(ownerList.get(i).getName().equals(ownerName))
				owner = ownerList.get(i);
		
		panel = new JPanel();
		try {
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		ownerFrame = new JFrame("Hotel Management System : " + ownerName);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		ownerFrame.setSize(new Dimension(700, 130));
		ownerFrame.setResizable(false);
		ownerFrame.setLocation((int) (screenSize.width * 0.25),
				(int) (screenSize.height * 0.25));
		ownerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ownerFrame.setLayout(new BorderLayout());

		createHotelAndRoomNumField();
		createButtons();

		ownerFrame.add(panel, BorderLayout.CENTER);
		ownerFrame.setVisible(true);
	}
	
	/**
	 * Void method that creates the ID and Password Fields and adds them to the
	 * panel.
	 */
	private void createHotelAndRoomNumField() 
	{
		JPanel passwordPanel = new JPanel();
		passwordPanel.setBackground(Color.white);
		JPanel idPanel = new JPanel();
		hotelField = new JTextField(20);
		hotelField.setEditable(true);
		hotelField.setCaretPosition(0);

		hotelField.setBorder(new LineBorder(Color.BLACK, 1, true));

		roomsField = new JTextField(20);
		roomsField.setEditable(true);
		roomsField.setCaretPosition(0);
		roomsField.setBorder(new LineBorder(Color.BLACK, 1, true));

		JTextArea idTxtArea = new JTextArea("Hotel Name :");

		idTxtArea.setEditable(false);
		idTxtArea.setHighlighter(null);

		idPanel.add(idTxtArea);
		idPanel.add(hotelField);

		idPanel.setBackground(Color.white);

		JTextArea passwordTxtArea = new JTextArea("Number of rooms :");
		passwordTxtArea.setEditable(false);
		passwordTxtArea.setHighlighter(null);
		passwordPanel.add(passwordTxtArea);
		passwordPanel.add(roomsField);
		panel.add(idPanel);
		panel.add(passwordPanel);
		panel.setBackground(Color.white);
	}

	/**
	 * Void methods that creates the buttons and adds them to the panel.
	 */
	public void createButtons() {
		JPanel buttonPanel = new JPanel(new FlowLayout());
		addHotelButton = new JButton("Add Hotel");
		addHotelButton.setActionCommand("Add");
		addHotelButton.addActionListener(this);
		removeHotelButton = new JButton("Remove Hotel");
		removeHotelButton.setActionCommand("Remove");
		removeHotelButton.addActionListener(this);
		JButton CreateButton = new JButton();
		CreateButton = new JButton("Logout");
		CreateButton.setActionCommand("Logout");
		CreateButton.addActionListener(this);
		JButton ViewButton = new JButton("View Hotels");
		ViewButton.setActionCommand("ViewHotels");
		ViewButton.addActionListener(this);
		buttonPanel.add(addHotelButton);
		buttonPanel.add(new JSeparator());
		buttonPanel.add(removeHotelButton);
		buttonPanel.add(CreateButton);
		buttonPanel.add(ViewButton);
		panel.add(new JSeparator());
		panel.add(new JSeparator());
		panel.add(new JSeparator());
		panel.add(buttonPanel);
		Container cp = ownerFrame.getContentPane();
		cp.setBackground(Color.white);
	}

	/**
	 * Action Performed method that overrides actionListener's actionPerformed
	 * method.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String c = e.getActionCommand();
		if (c.equals("Add")) // Add new hotel
		{
			try 
			{
				int numRooms = Integer.valueOf(roomsField.getText());
				if(numRooms < 1)
					throw new NumberFormatException();
				Hotel toAdd = new Hotel(hotelField.getText(), owner, numRooms);
				if(owner.addHotel(toAdd))
					JOptionPane.showMessageDialog(null, "Hotel added!", "Hotel Added Successfully",  JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null, "Hotel already exists!", "Hotel Not Added",  JOptionPane.INFORMATION_MESSAGE);	
			} catch(NumberFormatException nfe)
			{
				JOptionPane.showMessageDialog(null, "Invalid number of rooms!", "Hotel Not Added",  JOptionPane.INFORMATION_MESSAGE);
			}
			hotelField.setText("");
			roomsField.setText("");
		}
		else if (c.equals("Remove")) // remove hotel
		{
			if(owner.removeHotel(hotelField.getText()))
				JOptionPane.showMessageDialog(null, "Hotel removed!", "Hotel Removed Successfully",  JOptionPane.INFORMATION_MESSAGE);
			else
				JOptionPane.showMessageDialog(null, "Hotel not found!", "Hotel Not Removed",  JOptionPane.INFORMATION_MESSAGE);
			hotelField.setText("");
			roomsField.setText("");
		} 
		else if (c.equals("Logout")) // logout
		{
			new loginWindow(loginWindow.database);
			ownerFrame.dispose();
		}
		else if (c.equals("ViewHotels")) // logout
		{
			String allHotels = "";
			for(int i = 0; i < owner.getHotels().size(); i++)
			{
				Hotel currentHotel = owner.getHotels().get(i);
				allHotels += "Hotel :   " + currentHotel.getName() + ",   " + currentHotel.getTotalRooms() + " rooms \n";
			}
			JOptionPane.showMessageDialog(null, allHotels, "Displaying all" + ownerName + " hotels",  JOptionPane.INFORMATION_MESSAGE);
		}
	}
}
